﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Management;
 

namespace BySoft.Web.Utility
{
    public class FileHelper
    {
        /// <summary>
        /// 获取磁盘盘符
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllDisk()
        {
            List<string> list = new List<string>();
            ManagementObjectSearcher query = new ManagementObjectSearcher();
            query.Query = new ObjectQuery("select DriveType,Name from win32_logicaldisk");
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                if (mo["DriveType"].ToString() == "3")
                    list.Add(mo["Name"].ToString());
            }
            return list;
        }

        /// <summary>
        /// 获取文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFolder(string path)
        {
            List<string> list = new List<string>();
            try
            {
                string[] dirs = Directory.GetDirectories(path + "\\");
                foreach (string d in dirs)
                    list.Add(d.Substring(d.LastIndexOf('\\') + 1));
            }
            catch
            { }
            return list;
        }

        /// <summary>
        /// 检查是否存在该文件夹
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool CheckFolderExist(string path)
        {
            return Directory.Exists(path);
        }

        /// <summary>
        /// 检查是否存在该文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool CheckFileExist(string path)
        {
            return File.Exists(path);
        }

        /// <summary>
        /// 序列化对象并保存
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="path"></param>
        public static void SaveFile(DataSet ds, string path)
        {
            Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
            try
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, ds);
                stream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
        }

        /// <summary>
        /// 反序列化对象
        /// </summary>
        public static object ReadFile(string path)
        {
            Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            object obj = null;
            try
            {
                IFormatter formatter = new BinaryFormatter();
                obj = formatter.Deserialize(stream);
                stream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                stream.Close();
            }
            return obj;
        }

        /// <summary>
        /// 获取文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFiles(string path)
        {
            List<string> list = new List<string>();
            try
            {
                string[] dirs = Directory.GetFiles(path + "\\");
                foreach (string d in dirs)
                {
                    list.Add(d.Substring(d.LastIndexOf('\\') + 1));
                }
            }
            catch
            { }
            return list;
        }
    }
}
